package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.Testpaper;
import com.zengguang.testkaoshi.services.impl.TestpaperService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "testpaper")
@RestController
@CrossOrigin
public class TestpaperAction {
    @Autowired
    private TestpaperService testpaperService;

    @ApiOperation(value = "试卷表添加方法")
    @RequestMapping(value = "/addtestpaper",method = RequestMethod.GET)
    public ResponseMessage addtestpaper(Testpaper testpaper) {
        int isAdd = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (testpaper != null) {
            //添加角色数据
            isAdd = testpaperService.insertTestpaper(testpaper);
            if (isAdd == 1) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "试卷表修改方法")
    @RequestMapping(value = "/updatetestpaper",method = RequestMethod.GET)
    public ResponseMessage update(Testpaper testpaper) {
        int isUpadte = 0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (testpaper != null) {
            //添加试卷数据
            isUpadte = testpaperService.updateTestpaper(testpaper);
            if (isUpadte >0) {
                return Result.success("试卷表信息更新成功");
            } else {
                return Result.error("试卷表信息更新失败");
            }

        }
        return Result.error("试卷表信息更新失败");
    }

    @ApiOperation(value = "试卷表信息查询方法")
    @RequestMapping(value = "/selectAlltestpaper",method = RequestMethod.GET)
    public ResponseMessage selectAlltestpaper(Testpaper testpaper) {
        Map<String, List> map=new HashMap<>();
        List<Testpaper> list=testpaperService.selectAllTestpaper();
        if (!list.isEmpty()){
            map.put("list",list);
            return Result.success("200",map);
        }
        return Result.error("查询失败");
    }

    @ApiOperation("删除试卷表信息")
    @RequestMapping(value = "/deletetestpaper",method = RequestMethod.GET)
    public ResponseMessage deletetestpaper(int testpaperid){
        int temp=0;
        if (testpaperid!=0){
            temp=testpaperService.deleteTestpaper(testpaperid);
            if (temp>0) {
                return Result.success("删除试卷表信息成功",temp);
            }
            else {
                return Result.error("删除试卷表信息失败");
            }
        }

        return Result.error("试卷表信息编号不为0");
    }
}
